package chen.yi.component.code.generator.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.List;

/**
 * 属性类型映射
 * @author huat
 **/
@Getter
@AllArgsConstructor
public enum DatabaseTypeMappingEnums {
    // 数值类型
    TINYINT("tinyint", "Integer"),
    SMALLINT("smallint", "Integer"),
    INT("int", "Integer"),
    BIGINT("bigint", "Long"),
    FLOAT("float", "Float"),
    DOUBLE("double", "Double"),
    DECIMAL("decimal", "BigDecimal"),
    MEDIUMINT("mediumint", "Integer"),
    BIT("bit", "Boolean"),
    INTEGER("integer", "Integer"),

    // 字符串类型
    CHAR("char", "String"),
    VARCHAR("varchar", "String"),
    TEXT("text", "String"),
    LONG_TEXT("long text", "String"),
    BINARY("binary", "byte[]"),
    VARBINARY("varbinary", "byte[]"),
    BLOB("blob", "byte[]"),
    TINYBLOB("tinyblob", "byte[]"),
    LONG_BLOB("long blob", "byte[]"),
    MEDIUMBLOB("mediumblob", "byte[]"),
    ENUM("enum", "String"),

    // 日期时间类型
    DATE("date", "Date"),
    TIME("time", "Date"),
    DATETIME("datetime", "Date"),
    TIMESTAMP("timestamp", "Date"),
    YEAR("year", "Integer"),

    // JSON类型
    JSON("json", "String"),

    // 空间类型
    GEOMETRY("geometry", "Object"),
    POINT("point", "Object"),
    LINESTRING("linestring", "Object"),
    POLYGON("polygon", "Object"),
    GEOMETRYCOLLECTION("geometrycollection", "Object")
    ;
    /**
     * 数据库字段类型
     */
    private final String databaseType;
    /**
     * java属性类型
     */
    private final String javaType;

    /**
     * 根据数据库字段类型获取java属性类型
     * @param dbType 数据库类型
     * @return java类型
     */
    public static String getJavaTypeByDatabaseType(String dbType) {
        for (DatabaseTypeMappingEnums mapping : values()) {
            if (dbType.startsWith(mapping.getDatabaseType().toLowerCase())) {
                return mapping.getJavaType();
            }
        }
        return "String"; // 默认返回String类型
    }
}
